#pragma once
#include <iostream>
#include <unistd.h>
#include <string>
#include <vector>

using namespace std;

void work()
{

}

class channel
{
public:
    channel()
    {}

    channel(int fd, string& name, int pro_id)
            :_fd(fd),
             _name(name),
             _pro_id(pro_id)
             {}
    
    ~channel()
    {}

    int getfd()
    {
        return _fd;
    }

    string getname()
    {
        return _name;
    }

    int getpro_id()
    {
        return _pro_id;
    }

private:
    int _fd;      //管道文件fd
    string _name; //进程名字
    int _pro_id;  //进程id
};

class mul_process
{
public:
    mul_process()
    {}

    ~mul_process()
    {}

    void debug()
    {
        for(auto& e : _channel_table)
        {
            cout << "name: " << e.getname();
            cout << " _fd: " << e.getfd();
            cout << " _pro_id " << e.getpro_id() << endl;
        }
    }

    void Create(int sum)
    {
        //给每个子进程分配管道
        for(int i = 0 ; i < sum; i++)
        {
            int pro_pipe[2];
            
            //创建一个管道
            int ret = pipe(pro_pipe);
            if(ret == -1)
            {
                perror("pipe:");
                break;
            }

            pid_t id = fork();
            if(id == 0)
            {
                //子进程 r 
                close(pro_pipe[1]);
                sleep(100);
                exit(0);
            }

            //父进程 w
            close(pro_pipe[0]);
            string name = "process " + to_string(i);
            _channel_table.push_back( channel(pro_pipe[1],name,i) );           
        }
    }

private:
    vector<channel> _channel_table;
};

